package org.dalmatian.common.mybatis.config;


import cn.hutool.core.net.NetUtil;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.core.incrementer.DefaultIdentifierGenerator;
import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
import org.apache.ibatis.solon.annotation.Db;
import org.dalmatian.common.mybatis.handler.InjectionMetaObjectHandler;
import org.dalmatian.common.mybatis.interceptor.PlusDataPermissionInterceptor;
import org.noear.solon.Solon;
import org.noear.solon.annotation.Bean;
import org.noear.solon.annotation.Configuration;
import org.noear.solon.annotation.Inject;


/**
 * mybatis-plus配置类(下方注释有插件介绍)
 *
 * @author Lion Li
 */
@Configuration
public class MybatisPlusConfiguration {

    @Bean
    public void master(
        @Db("system") MybatisConfiguration cfg,
        @Db("system") GlobalConfig globalConfig,
        @Inject("${mybatis-plus.system.mapperPackage}") String mapperPackage) throws Exception {
        MybatisPlusInterceptor plusInterceptor = new MybatisPlusInterceptor();
        try {
            TenantLineInnerInterceptor tenant = Solon.context().getBean(TenantLineInnerInterceptor.class);
            if(tenant !=null)
            {
                plusInterceptor.addInnerInterceptor(tenant);
            }
        } catch (Exception ignore) {
        }
        plusInterceptor.addInnerInterceptor(paginationInnerInterceptor());
        plusInterceptor.addInnerInterceptor(optimisticLockerInnerInterceptor());
        plusInterceptor.addInnerInterceptor(dataPermissionInterceptor(mapperPackage));
        cfg.setCacheEnabled(false);
        cfg.addInterceptor(plusInterceptor);
    }



    public PlusDataPermissionInterceptor dataPermissionInterceptor(
        @Inject("${mybatis-plus.mapperPackage}") String mapperPackage) {
        return new PlusDataPermissionInterceptor(mapperPackage);
    }

    @Bean
    public MetaObjectHandler metaObjectHandler() {
        return new InjectionMetaObjectHandler();
    }

//    }

    /**
     * 分页插件，自动识别数据库类型
     */
    public PaginationInnerInterceptor paginationInnerInterceptor() {
        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
        // 分页合理化
        paginationInnerInterceptor.setOverflow(true);
        return paginationInnerInterceptor;
    }

    /**
     * 乐观锁插件
     */

    public OptimisticLockerInnerInterceptor optimisticLockerInnerInterceptor() {
        return new OptimisticLockerInnerInterceptor();
    }


    /**
     * 使用网卡信息绑定雪花生成器 防止集群雪花ID重复
     */
    @Bean
    public IdentifierGenerator idGenerator() {
        return new DefaultIdentifierGenerator(NetUtil.getLocalhost());
    }


}
